/**
 * Date:2021年4月29日 下午3:00:34
 * Copyright (c) 2021, www.bwbroad.com All Rights Reserved
 */

package com.app.common.config;

import java.math.BigInteger;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.time.ZoneId;
import java.time.format.DateTimeFormatter;
import java.util.Locale;
import java.util.TimeZone;

import org.springframework.boot.autoconfigure.condition.ConditionalOnMissingBean;
import org.springframework.boot.autoconfigure.jackson.Jackson2ObjectMapperBuilderCustomizer;
import org.springframework.context.annotation.Bean;
import org.springframework.context.annotation.Configuration;
import org.springframework.context.annotation.Primary;
import org.springframework.http.converter.json.Jackson2ObjectMapperBuilder;

import com.fasterxml.jackson.databind.DeserializationFeature;
import com.fasterxml.jackson.databind.ObjectMapper;
import com.fasterxml.jackson.databind.SerializationFeature;
import com.fasterxml.jackson.databind.module.SimpleModule;
import com.fasterxml.jackson.databind.ser.std.ToStringSerializer;
import com.fasterxml.jackson.datatype.jsr310.JavaTimeModule;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalDateTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.deser.LocalTimeDeserializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalDateTimeSerializer;
import com.fasterxml.jackson.datatype.jsr310.ser.LocalTimeSerializer;

/**
 * Description: json序列化配置 <br/>
 * Date:2021年4月29日 下午3:00:34 <br/>
 * 
 * @author xuejianxin
 */
@Configuration
public class JacksonConfig {

	@Bean
	public Jackson2ObjectMapperBuilderCustomizer customizer() {
		return builder -> {
			builder.locale(Locale.CHINA);
			builder.timeZone(TimeZone.getTimeZone(ZoneId.systemDefault()));
			builder.simpleDateFormat("yyyy-MM-dd HH:mm:ss");

			JavaTimeModule javaTimeModule = new JavaTimeModule();
			javaTimeModule.addSerializer(LocalDateTime.class,
					new LocalDateTimeSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
			javaTimeModule.addSerializer(LocalDate.class,
					new LocalDateSerializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
			javaTimeModule.addSerializer(LocalTime.class,
					new LocalTimeSerializer(DateTimeFormatter.ofPattern("HH:mm:ss")));
			javaTimeModule.addDeserializer(LocalDateTime.class,
					new LocalDateTimeDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd HH:mm:ss")));
			javaTimeModule.addDeserializer(LocalDate.class,
					new LocalDateDeserializer(DateTimeFormatter.ofPattern("yyyy-MM-dd")));
			javaTimeModule.addDeserializer(LocalTime.class,
					new LocalTimeDeserializer(DateTimeFormatter.ofPattern("HH:mm:ss")));

			builder.modules(javaTimeModule);
		};
	}

	@Bean
	@Primary
	@ConditionalOnMissingBean(ObjectMapper.class)
	public ObjectMapper jacksonObjectMapper(Jackson2ObjectMapperBuilder builder) {
		ObjectMapper objectMapper = builder.createXmlMapper(false).build();

		// 通过该方法对mapper对象进行设置，所有序列化的对象都将按改规则进行系列化
		// Include.Include.ALWAYS 默认
		// Include.NON_DEFAULT 属性为默认值不序列化
		// Include.NON_EMPTY 属性为 空（""） 或者为 NULL 都不序列化，则返回的json是没有这个字段的
		// Include.NON_NULL 属性为NULL 不序列化
		// objectMapper.setSerializationInclusion(JsonInclude.Include.NON_NULL);
		// 允许出现特殊字符和转义符
		// objectMapper.configure(MapperFeature.ALLOW_UNQUOTED_CONTROL_CHARS, true);
		// 允许出现单引号
		// objectMapper.configure(MapperFeature.ALLOW_SINGLE_QUOTES, true);
		objectMapper.disable(SerializationFeature.WRITE_DATES_AS_TIMESTAMPS);
		objectMapper.configure(DeserializationFeature.FAIL_ON_UNKNOWN_PROPERTIES, false);

		SimpleModule simpleModule = new SimpleModule();
		/**
		 * 将Long,BigInteger序列化的时候,转化为String
		 */
		simpleModule.addSerializer(Long.class, ToStringSerializer.instance);
		simpleModule.addSerializer(Long.TYPE, ToStringSerializer.instance);
		simpleModule.addSerializer(BigInteger.class, ToStringSerializer.instance);

		objectMapper.registerModule(simpleModule);

		return objectMapper;
	}
}